namespace CIS
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class AnnounceManagePage : Page
    {
        protected TextBox AnnounceContextTbx;
        protected TextBox AnnounceTitleTbx;
        protected DropDownList AreaDdl;
        protected CheckBox EditCHK;
        protected Image iAnnounceManage;
        protected CheckBox IsPublishedCHK;
        protected Label lbArea;
        protected Label lbContent;
        protected Label lbContentEdit;
        protected Label lbEdit;
        protected Label lbIssue;
        protected Label lbIssueTime;
        protected Label lbOperate;
        protected Label lbPublished;
        protected Label lbTitle;
        protected Label lbTitleEdit;
        protected UserPrincipal Principal = null;
        protected Repeater rptAnnounce;
        protected Button SaveBtn;

        private void AreaDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.FillPage();
        }

        private void FillPage()
        {
            this.rptAnnounce.DataSource = ManageAnnounce.GetAnnouncesInAdmin(new Guid(this.AreaDdl.SelectedValue));
            this.rptAnnounce.DataBind();
        }

        protected string GetStrInSetFile(string str)
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            return ManageSystemConfig.GetSystemSetLanguage("AnnounceManagePage", str, language, path);
        }

        protected string GetSubContent(string announce)
        {
            if (announce != null)
            {
                if (announce.Length > 30)
                {
                    return (announce.Substring(0, 30) + "...");
                }
                return announce;
            }
            return string.Empty;
        }

        private void InitializeComponent()
        {
            this.AreaDdl.SelectedIndexChanged += new EventHandler(this.AreaDdl_SelectedIndexChanged);
            this.rptAnnounce.ItemCommand += new RepeaterCommandEventHandler(this.rptAnnounce_ItemCommand);
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
            this.AreaDdl.Items.Clear();
            Area[] area = ManageArea.GetAreaAryViaId(this.Principal.SysUser.UserId);
            for (int i = 0; i < area.Length; i++)
            {
                ListItem tmpListItem = new ListItem();
                tmpListItem.Text = area[i].AreaName;
                tmpListItem.Value = area[i].AreaId.ToString();
                this.AreaDdl.Items.Add(tmpListItem);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (this.Principal.SysUser.IsAdmin == 0)
            {
                throw new ApplicationException(string.Format("User: {0} not has privilege to access this page.", this.Principal.SysUser.LoginName));
            }
            if (!base.IsPostBack)
            {
                this.EditCHK.Checked = false;
                this.InitPage();
                this.FillPage();
            }
        }

        private void rptAnnounce_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string cmd = e.CommandName;
            string strTmp = (string) e.CommandArgument;
            Guid announceId = new Guid(strTmp);
            switch (cmd)
            {
                case "delete":
                    ManageAnnounce.RemoveAnnounceViaId(announceId);
                    this.FillPage();
                    break;

                case "modify":
                {
                    Announce announce = ManageAnnounce.GetAnnounceViaId(announceId);
                    if (announce != null)
                    {
                        this.AnnounceTitleTbx.Text = announce.Title;
                        this.AnnounceContextTbx.Text = announce.Content;
                        this.IsPublishedCHK.Checked = announce.IsPublished;
                        this.EditCHK.Checked = true;
                        this.Page.RegisterHiddenField("announceId", announceId.ToString());
                    }
                    break;
                }
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            if (this.EditCHK.Checked)
            {
                try
                {
                    if (this.AnnounceTitleTbx.Text.Trim() == string.Empty)
                    {
                        this.AnnounceTitleTbx.Text = string.Empty;
                        this.AnnounceContextTbx.Text = string.Empty;
                        this.IsPublishedCHK.Checked = false;
                        this.EditCHK.Checked = false;
                        base.Response.Write("<script> alert('title不能为空') </script>");
                        return;
                    }
                    Guid announceId = new Guid(base.Request.Form["announceId"].ToString());
                    ManageAnnounce.UpdateAnnounce(announceId, this.AnnounceTitleTbx.Text.Trim(), this.AnnounceContextTbx.Text.Trim(), this.IsPublishedCHK.Checked);
                    this.AnnounceTitleTbx.Text = string.Empty;
                    this.AnnounceContextTbx.Text = string.Empty;
                    this.IsPublishedCHK.Checked = false;
                    this.EditCHK.Checked = false;
                }
                catch (Exception)
                {
                    return;
                }
            }
            else
            {
                if (this.AnnounceTitleTbx.Text.Trim() == string.Empty)
                {
                    base.Response.Write("<script> alert('title不能为空') </script>");
                    return;
                }
                ManageAnnounce.InsertNewAnnounce(this.AnnounceTitleTbx.Text.Trim(), this.AnnounceContextTbx.Text.Trim(), this.IsPublishedCHK.Checked, new Guid(this.AreaDdl.SelectedValue));
                this.AnnounceTitleTbx.Text = string.Empty;
                this.AnnounceContextTbx.Text = string.Empty;
                this.IsPublishedCHK.Checked = false;
                this.EditCHK.Checked = false;
            }
            this.FillPage();
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/CIS/LanguageConfig.xml");
            this.lbIssueTime.Text = ManageSystemConfig.GetSystemSetLanguage("AnnounceManagePage", "lbIssueTime", language, path);
            this.lbPublished.Text = ManageSystemConfig.GetSystemSetLanguage("AnnounceManagePage", "lbPublished", language, path);
            this.lbOperate.Text = ManageSystemConfig.GetSystemSetLanguage("AnnounceManagePage", "lbOperate", language, path);
            this.lbTitleEdit.Text = ManageSystemConfig.GetSystemSetLanguage("AnnounceManagePage", "lbTitleEdit", language, path);
            this.lbContentEdit.Text = ManageSystemConfig.GetSystemSetLanguage("AnnounceManagePage", "lbContentEdit", language, path);
            this.lbTitle.Text = ManageSystemConfig.GetSystemSetLanguage("AnnounceManagePage", "lbTitle", language, path);
            this.lbContent.Text = ManageSystemConfig.GetSystemSetLanguage("AnnounceManagePage", "lbContent", language, path);
            this.lbIssue.Text = ManageSystemConfig.GetSystemSetLanguage("AnnounceManagePage", "lbIssue", language, path);
            this.lbEdit.Text = ManageSystemConfig.GetSystemSetLanguage("AnnounceManagePage", "lbEdit", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("AnnounceManagePage", "SaveBtn", language, path);
            this.lbArea.Text = ManageSystemConfig.GetSystemSetLanguage("AnnounceManagePage", "lbArea", language, path);
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iAnnounceManage.ImageUrl = "images/title_AnnounceInfoManage.jpg";
                    break;

                case "en":
                    this.iAnnounceManage.ImageUrl = "images/title_AnnounceInfoManageEn.jpg";
                    break;
            }
        }
    }
}

